<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>高亮搜索 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/highlighting-intro.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/highlighting-intro.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/highlighting-intro.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/highlighting-intro.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="getting-started.html">基础入门</a></span>
»
<span class="breadcrumb-link"><a href="intro.html">你知道的, 为了搜索…​</a></span>
»
<span class="breadcrumb-node">高亮搜索</span>
</div>
<div class="navheader">
<span class="prev">
<a href="_phrase_search.html">« 短语搜索</a>
</span>
<span class="next">
<a href="_analytics.html">分析 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="highlighting-intro"></a>高亮搜索 (Highlighting Our Searches)<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/010_Intro/30_Tutorial_Search.asciidoc">edit</a>
</h2>
</div></div></div>
<p>许多应用都倾向于在每个搜索结果中 <em>高亮(highlight )</em> 文本片段，以便让用户知道该文档<em>为什么</em>符合查询条件。在 Elasticsearch 中检索出高亮片段也很容易。</p>
<p>再次执行前面的查询，但是增加一个新的 <code class="literal">highlight</code> 参数：</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">GET /megacorp/employee/_search
{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    },
    "highlight": {
        "fields" : {
            "about" : {}
        }
    }
}</pre>
</div>
<div class="sense_widget" data-snippet="snippets/010_Intro/30_Query_DSL.json"></div>

<p>当执行该查询时，返回结果与之前一样，但此时结果中还多了一个叫做 <code class="literal">highlight</code> 的部分。这个部分包含了 <code class="word">about</code> 字段匹配的文本片段，并以 HTML 标签 <code class="literal">&lt;em&gt;&lt;/em&gt;</code> 包裹：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
   ...
   "hits": {
      "total":      1,
      "max_score":  0.23013961,
      "hits": [
         {
            ...
            "_score":         0.23013961,
            "_source": {
               "first_name":  "John",
               "last_name":   "Smith",
               "age":         25,
               "about":       "I love to go rock climbing",
               "interests": [ "sports", "music" ]
            },
            "highlight": {
               "about": [
                  "I love to go &lt;em&gt;rock&lt;/em&gt; &lt;em&gt;climbing&lt;/em&gt;" <a id="CO6-1"></a><i class="conum" data-value="1"></i>
               ]
            }
         }
      ]
   }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO6-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>原始文本中的高亮片段</p>
</td>
</tr>
</table>
</div>
<p>关于高亮搜索片段，可以在 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/2.4/search-request-highlighting.html" class="ulink" target="_top">highlighting reference documentation</a> 了解更多信息。</p>
</div>
<div class="navfooter">
<span class="prev">
<a href="_phrase_search.html">« 短语搜索</a>
</span>
<span class="next">
<a href="_analytics.html">分析 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>